/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.mlu.jdbc.simple;

import java.io.FileNotFoundException;
import java.io.InputStream;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * Simple jdbc implementation base class
 * 
 * Generally you would extend this class 
 *
 * @author Mlungisi Sincuba
 * @since 1.0
 */
public class BaseSimpleJdbcImpl {
    protected static Logger logger = LoggerFactory.getLogger(BaseSimpleJdbcImpl.class);
    protected SimpleJdbc jdbc;
    protected DataSource dataSource;
    
    /**
     * Uses db.properties from project path or class path, starting at project path;
     * 
     */
    public BaseSimpleJdbcImpl() {
        if (dataSource == null) {
            try {
                dataSource = DataSourceDetails.getDataSource();
            } catch (FileNotFoundException ex) {
                if (dataSource == null) {
                    dataSource = DataSourceDetails.getDataSourceFromClassPath();
                }
                logger.debug("File 'db.properties' was not found");
            }
            if (dataSource == null) {
                dataSource = DataSourceDetails.getDataSourceFromClassPath();
            }
        }
        logger.debug("Using datasource: " + dataSource);
        if (jdbc == null) {
            jdbc = new SimpleJdbc(dataSource);
        }
    }
    
    /**
     * 
     * Used main on stand-alone applications
     * 
     * @param fileName properties file on project path
     */
    public BaseSimpleJdbcImpl(String fileName) {
        if (fileName == null) {
            fileName = "db.properties";
        }
        if (dataSource == null) {
            try{
                dataSource = DataSourceDetails.getDataSource(fileName);
            } catch(FileNotFoundException ex) {
                if (dataSource == null) {
                    fileName = fileName.substring(0, fileName.indexOf("."));
                    dataSource = DataSourceDetails.getDataSourceFromClassPath(fileName);
                }
                logger.debug("File " + fileName + " was not found");
            }
            
        }
        logger.debug("Using datasource: " + dataSource);
        if (jdbc == null) {
            jdbc = new SimpleJdbc(dataSource);
        }
    }

    /**
     * 
     * @param input which contains properties file from either class path or WEB-INF path
     * 
     * Usage example:
     * InputStream input = getServletContext().getResourceAsStream("/WEB-INF/db.properties");
     * BaseSimpleJdbcSubclass x = new BaseSimpleJdbcSubclassImpl(input);
     * 
     * 
     */
    public BaseSimpleJdbcImpl(InputStream input) {
        if (dataSource == null) {
            dataSource = DataSourceDetails.getDataSourceForWeb(input);
        }
        logger.debug("Using datasource: " + dataSource);
        if (jdbc == null) {
            jdbc = new SimpleJdbc(dataSource);
        }
    }
}
